package com.oocl.aeat.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oocl.aeat.dto.JsonResult;
import com.oocl.aeat.pojo.Verify;
import com.oocl.aeat.redis.VerifyCache;
import com.oocl.aeat.service.VerifyService;
import com.oocl.aeat.service.factory.VerifyServiceFactory;
import com.oocl.aeat.util.Const;
import com.oocl.aeat.util.JsonUtil;

@WebServlet("/sec/verify")
public class VerifyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private VerifyService verifyService;
    public VerifyServlet() {
    	verifyService = VerifyServiceFactory.getInstance();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		Map<String, Verify> map = VerifyCache.getCache();
		List<Verify> list = new ArrayList<Verify>();
		Set<String> keys = map.keySet();
		for(String key : keys){
			list.add(map.get(key));
		}
		JsonResult result = new JsonResult(Const.JSON_SUCCESS, new Date().getTime()+"", list);
		out.write(JsonUtil.getJsonFromObject(result));
		out.flush();
	}

	/**
	 * pass / refuse
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		String reason = request.getParameter("reason");
		String idStr = request.getParameter("id");
		String mid = request.getParameter("mid");
		String type = request.getParameter("type");
		Integer id = 0;
		try {
			if(id!=null){
				id = Integer.parseInt(idStr);
			}
		} catch (Exception e) {
			JsonResult result = new JsonResult(Const.JSON_FAIL, "id format error", null);
			out.write(JsonUtil.getJsonFromObject(result));
			out.flush();
    		return;
		}
		//查询数据库最新的数据，是否与页面的审核数据一致。
		//通过比较ID判断
		Verify lastlyVerify = verifyService.queryVerifyLastlyByMid(mid);
		if(lastlyVerify!=null){
			//如果是一致，则进行更新状态操作，否则重定向到审核页，提醒用户。
			if(lastlyVerify.getId().equals(id)){
				//执行批准操作
				if("pass".equals(type)){
					reason = "审核通过";
					lastlyVerify.setState(Const.STATE_SUCCESS);	
				//执行驳回操作
				}else if("refuse".equals(type)){
					lastlyVerify.setState(Const.STATE_FAIL);
				}
				lastlyVerify.setVerifyTime(new Date().getTime());
				lastlyVerify.setMsg(reason);
				if(verifyService.updateVerifyState(lastlyVerify)){
					JsonResult result = new JsonResult(Const.JSON_SUCCESS, "success", null);
					out.write(JsonUtil.getJsonFromObject(result));
					out.flush();
					return;
				}else{
					JsonResult result = new JsonResult(Const.JSON_FAIL, "Update fail.", null);
					out.write(JsonUtil.getJsonFromObject(result));
					out.flush();
					return;
				}
			}else{
				JsonResult result = new JsonResult(Const.JSON_UPDATE, "The data has been updated. Please refresh and retry.", null);
				out.write(JsonUtil.getJsonFromObject(result));
				out.flush();
				return;
			}
		}else{
			JsonResult result = new JsonResult(Const.JSON_NOTFOUND, "Mid cannot found.", null);
			out.write(JsonUtil.getJsonFromObject(result));
			out.flush();
			return;
		}
	}

}
